package com.uxhuanche.ui.widgets.dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import com.uxhuanche.ui.R;
import com.uxhuanche.ui.base.DensityUtils;


/**
 * @author Administrator on 2015/7/21.
 */
public class NetWaitDialog extends BaseDialog {
    int size = 70;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dialog_net_wait, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void setShowPosition(WindowManager.LayoutParams lp) {
        super.setShowPosition(lp);
        lp.width = DensityUtils.dip2px(getContext(), size);
        lp.height = DensityUtils.dip2px(getContext(), size);
    }

    public void show(FragmentActivity activity) {
        setOnOutAndBackCancel(false, false);
        FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
        transaction.add(this, null);
        transaction.commitAllowingStateLoss();
    }

    public void show(Fragment fragment) {
        setOnOutAndBackCancel(false, false);
        FragmentTransaction transaction = fragment.getChildFragmentManager().beginTransaction();
        transaction.add(this, null);
        transaction.commitAllowingStateLoss();
    }


    /**
     * 简化关闭异常处理
     *
     * @param dial
     */
    public static void close(NetWaitDialog dial) {
        if (dial != null && !dial.isRemoving()) {
            try {
                dial.dismissAllowingStateLoss();
            } catch (Exception ex) {
                //nothing
            }
        }
    }

    /**
     * 简化 生成代
     * //FIXME   后续优化，将Dialog 生命周期和宿主绑定
     *
     * @param old
     * @param control
     */

    public static NetWaitDialog getAndShow(NetWaitDialog old, FragmentActivity control) {
        if (old != null) {
            NetWaitDialog.close(old);
        }
        old = new NetWaitDialog();
        old.setCancelable(false);
        old.show(control);
        return old;
    }

    public static NetWaitDialog getAndShow(NetWaitDialog old, Fragment fragment) {
        if (old != null) {
            NetWaitDialog.close(old);
        }
        old = new NetWaitDialog();
        old.setCancelable(false);
        old.show(fragment);
        return old;

    }


}
